/**
 * @author Rudy Kaplan Depena (The University of Texas at Austin)
 */
package com.utaustin.mirankerlab.ultrawrap.compiler.components;

import com.utaustin.mirankerlab.ultrawrap.compiler.main.SparqlToSql;

/** A Component can be a variable, iri reference, type, or literal */
public class Component {
	
	//instance variables
	private String rawImg;
	private String sanitizedImg;
	private int tripleId;
	private boolean subject;
	private boolean subjectKey;
	private boolean predicate;
	private boolean object;
	private boolean objectKey;
	
	/**
	 * 
	 * @param _rawImg
	 * @param _tableId
	 */
	public Component(String _rawImg, int _tripleId){
		rawImg = _rawImg;
		sanitizedImg = sanitizeString(rawImg);
		tripleId = _tripleId;
		subject = false;
		subjectKey = false;
		predicate = false;
		object = false;
		objectKey = false;
	}
	
	/**
	 * Returns the raw image of the Component
	 * @return the rawImg
	 */
	public String getRawImg() {
		return rawImg;
	}

	/**
	 * Returns the sanitized image of the Component
	 * @return the sanitizedImg
	 */
	public String getSanitizedImg() {
		return sanitizedImg;
	}

	/**
	 * Returns the triple id so we know 
	 * which triple the component belongs to
	 * @return tripleId
	 */
	public int getTripleId(){
		return tripleId;
	}
	
	/**
	 * Return true if subject, else false
	 * @return the subject
	 */
	public boolean isSubject() {
		return subject;
	}

	/**
	 * Set subject
	 */
	public void setSubject() {
		this.subject = true;
		
		//turn all others false, because you can only be one
		//either subj, subjKey, pred, obj, objKey
		subjectKey = false;
		predicate = false;
		object = false;
		objectKey = false;
	}

	/**
	 * Return true if subject key, else false
	 * @return the subjectKey
	 */
	public boolean isSubjectKey() {
		return subjectKey;
	}

	/**
	 * Set subject key
	 */
	public void setSubjectKey() {
		this.subjectKey = true;
		
		//turn all others false, because you can only be one
		//either subj, subjKey, pred, obj, objKey
		subject = false;
		predicate = false;
		object = false;
		objectKey = false;
	}

	/**
	 * Return true if predicate, else false
	 * @return the predicate
	 */
	public boolean isPredicate() {
		return predicate;
	}

	/**
	 * Set predicate
	 */
	public void setPredicate() {
		this.predicate = true;
		
		//turn all others false, because you can only be one
		//either subj, subjKey, pred, obj, objKey
		subject = false;
		subjectKey = false;
		object = false;
		objectKey = false;
	}

	/**
	 * Returns true if object, else false
	 * @return the object
	 */
	public boolean isObject() {
		return object;
	}

	/**
	 * Set object
	 */
	public void setObject() {
		this.object = true;
		
		//turn all others false, because you can only be one
		//either subj, subjKey, pred, obj, objKey
		subject = false;
		subjectKey = false;
		predicate = false;
		objectKey = false;
	}

	/**
	 * Returns true if object key, else false
	 * @return the objectKey
	 */
	public boolean isObjectKey() {
		return objectKey;
	}

	/**
	 * Set object key
	 */
	public void setObjectKey() {
		this.objectKey = true;
		
		//turn all others false, because you can only be one
		//either subj, subjKey, pred, obj, objKey
		subject = false;
		subjectKey = false;
		predicate = false;
		object = false;
	}

	/**
	 * Used to provide a sanitized image for SQL query
	 * @return sanitized Image
	 */
	//TODO must test this, I haven't done that yet
	private String sanitizeString(String rawImg){
		
		//If I have a ':', then image is iri reference
		if(rawImg.contains(":")){ 
			String tag = rawImg.substring(0, rawImg.indexOf(':') + 1);
			if(SparqlToSql.prefixMap.doesTagExist(tag))
				return rawImg.replaceAll(tag, SparqlToSql.prefixMap.getIRI(tag));
		}
		
		//If I have a '?', then image is a variable
		if(rawImg.charAt(0) == '?'){
			return rawImg.substring(1);
		}
		
		//If I have an "a", then image is an rdf type
		if(rawImg.equals("a")){
			return sanitizeString("rdf:type");
		}
		
		//else image is a literal
		return rawImg;
	}
	
	/**
	 * Returns true if variable, otherwise false
	 * @return true if variable, otherwise false
	 */
	public boolean isVar(){
		return rawImg.charAt(0) == '?';
	}
	
	public String spo(){
		if(subject) return "s";
		if(subjectKey) return "spk";
		if(predicate) return "p";
		if(object) return "o";
		if(objectKey) return "opk";
		return null;
	}
	
	/**
	 * Returns the sanitized image of the Component
	 * @return (sanitizedImg, spo, tripleId)
	 */
	//TODO This may change
	public String toString(){
		if(subject){
			return "( " + sanitizedImg + ", " + "subj" + ", " + "tripleId = "  
						+ tripleId + " )";
		}
		else if(subjectKey){
			return "( " + sanitizedImg + ", " + "subjKey" + ", " + "tripleId = "  
			+ tripleId + " )";
		}
		else if(predicate){
			return "( " + sanitizedImg + ", " + "pred" + ", " + "tripleId = "  
			+ tripleId + " )";
		}
		else if(object){
			return "( " + sanitizedImg + ", " + "obj" + ", " + "tripleId = "  
			+ tripleId + " )";
		}
		else if(objectKey){
			return "( " + sanitizedImg + ", " + "objKey" + ", " + "tripleId = "  
			+ tripleId + " )";
		}
		return "( " + sanitizedImg + ", " + "noSPO" + ", " + "tripleId = "  
		+ tripleId + " )";
	}
	
}
